package cn.agiledata.bank.scores.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;

import cn.agiledata.bank.common.dao.HqlPageListTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.scores.form.HisScoresQueryBean;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class HisScoresQueryPageListPage extends HqlPageListTemplate {
	private HisScoresQueryBean bean;
	
	public HisScoresQueryPageListPage(HisScoresQueryBean bean) {
		// TODO Auto-generated constructor stub
		this.setSessionFactory(
				(SessionFactory)IPBAppFactory.getObj("sessionFactory"));
		this.bean = bean;
	}

	public String getHql() {
		// TODO Auto-generated method stub
		String hSql = " from ScoresHis s where s.account.id=:id ";
		//add by zxj, 2007-10-9
		hSql += " and  s.scoresAmount!=0";
		if (StringUtils.isNotEmpty(bean.getScoresSource())){
			//因为查询页面上把用户升级添加到了查询条件操作类型下拉列表里，需在此处做如下处理
			if (bean.getScoresSource().equals("upgrade")) {
				hSql += " and s.scoresActivityId is not null ";
			} else {
				hSql += " and s.scoresRules.scoresSource.id=:scoresSource ";
			}
		}
		if (StringUtils.isNotEmpty(bean.getBeginDate()))
			hSql += " and s.updateTime>=:startDate ";
		if (StringUtils.isNotEmpty(bean.getEndDate()))
			hSql += " and s.updateTime<=:endDate ";
		hSql += " order by s.id desc, s.updateTime desc";
		return hSql;
	}

	public String getPageRecord() {
		// TODO Auto-generated method stub
		return String.valueOf(Constant.PAGESIZE);
	}

	public Map getParams() {
		// TODO Auto-generated method stub
		String format ="yyyy-MM-dd HH:mm:ss";
	
		Map map = new HashMap();
		if (StringUtils.isNotEmpty(bean.getAccountId())) {
			map.put("id", new Long(bean.getAccountId()));
		}
		if (StringUtils.isNotEmpty(bean.getScoresSource())) {
			if (bean.getScoresSource().equals("upgrade")) {
				
			} else {
				map.put("scoresSource", new Long(bean.getScoresSource()));
			}
		}
		if (StringUtils.isNotEmpty(bean.getBeginDate())) {
			map.put("startDate", DateUtil.string2Date(bean.getBeginDate()+" 00:00:00",format));
		}
		if (StringUtils.isNotEmpty(bean.getEndDate())) {
			map.put("endDate", DateUtil.string2Date(bean.getEndDate()+" 23:23:59",format));
		}
		return map;
	}

	public String getStartPage() {
		// TODO Auto-generated method stub
		return bean.getStartPage();
	}

}
